package com.rst.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.rst.model.Subject;

@Repository
@Transactional
public class SubjectDAO {

	@Autowired
	private SessionFactory sessionFactory;

	public void saveSubject(Subject subject) {
		 sessionFactory.getCurrentSession().saveOrUpdate(subject);	
		}

	public Subject getSubjectBySubjectId(Long subjectId) {
	    Session session = sessionFactory.getCurrentSession();
	    Subject subject = (Subject)session.get(Subject.class, subjectId);
		return subject;
	}

	@SuppressWarnings("unchecked")
	public List<Subject> getSubjectListBySectionId(Long sectionId) {
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM Subject s WHERE s.sectionId = :sectionId  AND s.status = :status  ";
	    	
	    Query query = session.createQuery(str);
	    query.setParameter("status",  Subject.ACTIVE);
	    query.setParameter("sectionId",  sectionId);
		return query.list();
	}
}




